Skip to content

fix: keep the story on screen in the swipe viewer, load past Algolia's 1,000-result cap, and two smaller fixes - #288

Merged
therealsama merged 6 commits into
mainfrom
fix/verified-thread-and-swipe-bugs
Oct 5, 2026
Merged

therealsama merged 6 commits into
mainfrom
fix/verified-thread-and-swipe-bugs

Conversation

@therealsama

@therealsama therealsama commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes six bugs found in an audit. Three were reproduced in the browser against the unfixed code (the new e2e tests below), the two 1,000-result caps were confirmed against the live Algolia API, and the stale-metadata one came from reading the code. Each commit is one fix with its tests.

Mobile swipe viewer

  • A linked story that isn't in the Top feed was lost on the first swipe. Opening /#/item/:id directly mounted the viewer inside MobileItemResolver. The first swipe writes { from: 'top' } to the URL state, which the wrapper maps to its own SwipeStoryViewer, so React replaced the resolver's viewer with a new one that started from the Top feed. A shared Ask HN post or an older story was gone when you swiped back, and you landed on a different story. The wrapper now resolves the item type itself and renders the same keyed element as the { from: 'top' } branch, so the first swipe keeps the viewer.
  • A feed refresh could change the story under the reader. A feed opened from a stale cache refreshes in the background, and the refresh replaced every story. On a slow connection it landed after the first swipes, so the panel index stayed put while the story at it changed. useInfiniteStories now takes the ids the reader has reached (keepOnRevalidate), and a refresh keeps only those stories, followed by the fresh ones that aren't already there. The swipe viewer reports them from the first panel through the one on screen, so the story on screen stays even at index 0, and a linked story it moved to the front keeps only itself. The feed cache still gets the fresh page as fetched. The feed counts as fresh once the page arrives, rather than once it adds a story: with stories kept, every fetched one can already be there, and the swipe viewer calls loadMore for as long as the feed is marked as cached. The list is rebuilt from the kept stories at that point too, even when the page brings nothing new. The desktop list caches every story it loaded, so a phone that browsed the list in landscape (the desktop layout) and then read in portrait had cached stories past the kept ones. They stayed in the list, but they were no longer in the set of seen ids, so the next page showed them a second time.
  • 404 and "Item not found" pages kept the swipe viewer's page styles. index.html adds the swipe-mode class before React loads, but its route regex ended in an optional group with no end anchor, so it matched every #/ route. Only UserProfile cleared it, so a phone that opened a 404, /#/item/abc, or an empty /#/from/ directly got overflow: clip and no scrollbar on a page with no viewer. The regex now lists only the routes App.tsx gives a viewer.

Comments and story data

  • Threads with more than 1,000 comments stopped at 1,000. Algolia search returns at most 1,000 hits. When the first page shows the cap, comments now also come from Algolia's /items endpoint, which returns the whole tree in one request. The tree misses some comments that search has, so the two are merged rather than one replacing the other. If the tree request fails, the search results are used as before. On a 2,530-comment thread (38309611), search returned 1,000, the tree 2,450, and 47 were only in search. The paging loop also stops at the last page search reports instead of asking for the empty page after it.
  • A cached story's title, points and comment count were never refreshed. Revalidating a stale cached item fetches its comments, which reads the item from Firebase too, but only its text was kept. The cached title, points and comment count stayed up to 24 hours old and were written back as fresh. They now come from the fetched item. The type stays the local one, because Firebase calls Ask/Show posts story, and a fetched item with no title (a deleted story) changes only the body, as before.

Domain and user lists

  • Domain and user lists stopped at 1,000 stories. Both page through Algolia's search_by_date, which returns at most 1,000 hits and caps nbPages to match. So /#/from/github.com (over 460,000 stories) and the submissions of anyone with more than 1,000 stories ended with no sign there was more. Past the last page, the next request now starts again at page 0 with created_at_i at or before the oldest hit so far. Results are newest first, so this continues where the list stopped; the boundary story comes back once and the existing dedup drops it.

Testing

  • npm run typecheck, npm run lint and npm run test:run pass on every commit, not just the tip (1091 tests, 19 new).
  • npm run e2e (chromium, firefox, Mobile Safari, Mobile Chrome, retries off): 932 tests, 42 skipped. Two full runs passed 888 and 889. Each failure happened in one run only and passed when rerun: two Mobile Safari axe scans hit their 60 s timeout, and a Firefox home page stayed on its loading skeleton for 10 s before any story arrived from the mock.
  • New e2e tests:
    • A linked Ask HN story is still there after swiping away and back (mobile-swipe.spec.ts).
    • A Top refresh that lands after a swipe leaves the same story on screen (mobile-swipe.spec.ts).
    • A link to the last story of a cached Top feed refreshes it without paging back through older days (mobile-swipe.spec.ts).
    • A refresh with no new stories, under a cache longer than one page, drops the cached stories past the reader, so the next page doesn't repeat them (mobile-swipe.spec.ts).
    • The pre-React swipe-mode class is set for /#/best, /#/item/12345, /#/from/github.com/rust-lang and /#/submitted/pg, and not for /#/user/pg, /#/no-such-page, /#/item/abc or /#/from/ (mobile-breakpoint.spec.ts).
  • With main's source for these fixes, the e2e tests that check a bug (the two swipe tests and the four "not set" routes) failed on all four browsers, 24/24. The four "set" routes pass on main too, since the old regex matched everything. They stop the new regex from leaving out a viewer route. The paging test passes on main, which never had that problem: it failed on all four browsers against this PR's first version, where a refresh that brought no new story left the feed marked as cached and the viewer kept requesting older days. The no-new-stories test failed on all four browsers against this PR's previous version, which didn't rebuild the list, and passed 40/40 with the fix.
  • 15 of the 19 new unit tests fail with their fix reverted. The other 4 cover the fallbacks and pass on main too: the tree request failing, a thread under the cap making no tree request, a deleted story keeping its cached title, and a refresh with nothing kept replacing the list. One existing test changed: a restored Ask HN item now also takes its points from the fetch.
  • The e2e Firebase mock now has its own copy of each story that only the Algolia mocks listed. It answered them with a generic Item <id>, which the swipe panel now shows once the comments load. This is part of the stale-metadata commit.
  • The e2e swipe helper now waits up to 2 s for a swipe to finish before sending it again, up from 500 ms. On Mobile Safari under load some swipes finished later than that, and the retry moved the reader a panel further than asked: the refresh test failed 5 of 40 runs from it. With the change, the two refresh tests passed 80/80 on Mobile Safari and 120/120 on the other three browsers. This is part of the refresh commit.
  • In one earlier full run, three Chromium tests in mobile-swipe.spec.ts, one of them unchanged by this PR, rendered the desktop layout because the emulated (pointer: coarse) and (hover: none) query read false. It didn't come back in 90 repeated runs of that spec or in the next full run.

Not changed / known limitations

  • Polls don't show their options. The app never reads a poll's parts. This was found in the same audit and is left for a separate change.
  • Merging the tree with search isn't guaranteed to get every comment on a very large thread: on the 2,530-comment thread the two together give 2,497. I didn't check whether the rest are dead or deleted comments that neither endpoint returns.
  • A refresh whose page comes back with no stories (every story read failing) leaves the feed marked as cached, and the viewer pages through every older day. That happens on main too and is fixed separately in fix: treat a feed refresh with no stories as failed #289, which touches the same lines in useInfiniteStories.ts.
  • Desktop doesn't report reached stories, so a desktop feed refresh still replaces the whole list, as before.
  • UserProfile still turns swipe-mode off on mount. Direct loads no longer need it, but it's kept as a safety net.
  • If more than 1,000 stories share the oldest hit's created_at_i, the cursor can't move past that second, so the list ends there instead of repeating.

A direct link to /item/:id mounted the story viewer inside
MobileItemResolver. The first swipe writes { from: 'top' } to the URL
state, which the wrapper maps to its own SwipeStoryViewer, so React
replaced the resolver's viewer with a new one. The new viewer started
from the Top feed, so a linked story that isn't in it (an Ask HN post,
an older story) was gone when you swiped back, and you landed on the
wrong story.

The wrapper now looks up the item type itself and renders the same
keyed viewer element as the { from: 'top' } branch, so the first swipe
keeps the instance.
Algolia search returns at most 1,000 hits, so the comment fetch stopped
there: a 2,530-comment thread showed 1,000. When the first page shows
the cap, the comments now also come from the /items endpoint, which
returns the whole tree in one request. It misses a few comments that
search has (47 on that thread), so its comments are added to the search
results rather than replacing them. If it fails, the search results are
used as before.

The loop also stops at the last page search reports instead of asking
for one more empty page.
useItemWithComments revalidates a stale cached item by fetching its
comments, and that fetch reads the item from Firebase too, but only its
text was used. The cached title, points and comment count stayed up to
24 hours old and were written back as fresh, so the next visit kept
them too. This shows on a reload, a bookmark, Back to a story, or a
story opened before the feed's prefetch reached it.

They now come from the fetched item. The type stays the local one,
because Firebase calls Ask/Show posts 'story', and a fetched item
without a title (a deleted story) changes nothing but the body, as
before.

The e2e Firebase mock now has its own copy of each story that only the
Algolia mocks listed, with the same title and counts. It used to answer
them with a generic "Item <id>", which the swipe panel now shows once
the comments load.
@therealsama
therealsama requested a review from kplachkov as a code owner October 5, 2026 14:56
@therealsama
therealsama force-pushed the fix/verified-thread-and-swipe-bugs branch from 8f7b010 to 491b221 Compare October 5, 2026 16:09
A feed opened from its cache refreshes in the background, and the
refresh replaced every story. The swipe viewer starts it at mount, but
on a slow connection it lands after the first swipes: the panel's index
stayed the same while the story under it changed, with no input from
the reader.

useInfiniteStories now takes the ids the reader has reached
(keepOnRevalidate), and a refresh keeps those stories, followed by the
fresh ones that aren't already there. The swipe viewer reports them on
each change, from the first story through the one on screen, so the
story on screen stays even at index 0. The feed cache still gets the
fresh page as fetched. Desktop passes nothing, so its refresh still
replaces the whole list.

The feed counts as fresh once the page arrives, rather than once it adds
a story: with stories kept, every fetched one can already be there, and
the swipe viewer calls loadMore for as long as the feed is marked as
cached. The list is rebuilt from the kept stories at that point too,
even with nothing new on the page. The desktop list caches everything
it loaded, and the cached stories past the kept ones are no longer in
seenIds, so the next page would show them a second time.

The e2e swipe helper now waits up to 2 s for a swipe to finish before
sending it again, up from 500 ms. On Mobile Safari under load some
swipes finished later than that, and the retry then moved the reader a
panel further than asked; the new refresh test failed about one run in
ten because of it.
The domain and user lists page through Algolia's search_by_date, which
returns at most 1,000 hits and caps nbPages to match. Their 'has more'
check ended at that last page, so /from/github.com and the submissions
of anyone with more than 1,000 stories stopped with no sign that more
existed.

Past the last page, the next request now starts over at page 0 with
created_at_i at or before the oldest hit so far. Results are newest
first, so that picks up where the list stopped; the boundary story
comes back once and the existing dedup drops it.
index.html adds the swipe-mode class before React loads, for routes that
will mount a swipe viewer. Its regex ended in an optional group with no
end anchor, so it matched every '#/' route. Only UserProfile cleared the
class again, so a phone that opened a 404, /item/abc, or an empty /from/
or /submitted/ directly kept the swipe viewer's body styles
(overflow: clip, no scrollbar) on a page that has no viewer.

The regex now lists the routes App.tsx gives a viewer: the feeds, a
numeric /item/, and a non-empty /from/ or /submitted/.
@therealsama
therealsama merged commit 62c2778 into main Oct 5, 2026
4 checks passed
@therealsama
therealsama deleted the fix/verified-thread-and-swipe-bugs branch October 5, 2026 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants